﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DevExpress.Web.Mvc;
using EnterpriseFund.Common;
using Newtonsoft.Json;
using DevExpress.Web.ASPxGridView;
using System.Web.UI;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;


namespace EnterpriseFund.Controllers
{
    public class QueryFIssueNoController : Controller
    {
        //
        // GET: /QueryFIssueNo/

        private string IsNull(string s1, string s2)
        {
            return string.IsNullOrEmpty(s1) ? s2 : s1;
        }

        public string GetWhereString()
        {
            //绑定值

            //var FTaxClassAllowanceName = IsNull(ComboBoxExtension.GetValue<string>("FTaxClassAllowanceName"), Request["FTaxClassAllowanceName"]);
            var nsrsbh = Request["Nsrsbh"];
            var nsrbm = Request["Nsrbm"];
            var nsrmc = Request["Nsrmc"];
            var fIssueNo = Request["FIssueNo"];

            ViewData["fIssueNo"] = fIssueNo;
            ViewData["nsrbm"] = nsrbm;
            ViewData["nsrmc"] = nsrmc;
            ViewData["nsrsbh"] = nsrsbh;

            var filter = "";
            if (!string.IsNullOrEmpty(nsrsbh))
                filter += string.Format("and nsrsbh like '%{0}%'", nsrsbh);
            if (!string.IsNullOrEmpty(nsrbm))
                filter += string.Format("and nsrbm like '%{0}%'", nsrbm);
            if (!string.IsNullOrEmpty(nsrmc))
                filter += string.Format("and nsrmc like '%{0}%'", nsrmc);


            if (!string.IsNullOrEmpty(fIssueNo))
            {
                filter += string.Format(" and FIssueNo = '{0}' ", fIssueNo);
            }


            return filter;

        }

        public ActionResult ReportViewerPartial()
        {
            var strWhere = GetWhereString();
            var zsxx = new BLL.QueryFIssueNo().GetModelList(strWhere);
            foreach (Model.QueryFIssueNo q in zsxx)
            {
                if (!string.IsNullOrEmpty(q.FAmount.ToString()))
                {
                    q.FAmount = decimal.Parse(q.FAmount.ToString()) / 10000;
                }
            }

            MVCReportGeneratonHelper generator = new MVCReportGeneratonHelper();
            generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
            generator.CustomizeColumn += new CustomizeColumnEventHandler(generator_CustomizeColumn);
            XtraReport report = generator.GenerateMVCReport(OperateSession.GetFromCache("QueryFIssueNo") as MVCxGridViewState, zsxx);
            //  XtraReport report = generator.GenerateMVCReport(gridViewState, dt);

            //report.PaperKind = System.Drawing.Printing.PaperKind.A4;

            this.ViewData["rpt"] = report;

            return PartialView("ReportViewerPartial");

        }
        void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
        {


            for (int i = 0; i < e.ColumnsInfo.Count; i++)
            {

                if (e.ColumnsInfo[i].FieldName=="FFileNo")
                {
                    e.ColumnsInfo[i].ColumnWidth=  60;
                }
                 if (e.ColumnsInfo[i].FieldName=="FIssueNo")
                {
                    e.ColumnsInfo[i].ColumnWidth= 80 ;
                }
                 if (e.ColumnsInfo[i].FieldName=="NSRSBH")
                {
                    e.ColumnsInfo[i].ColumnWidth= 170 ;
                 }
                  if (e.ColumnsInfo[i].FieldName=="NSRBM")
                {
                    e.ColumnsInfo[i].ColumnWidth=  80 ;
                 }

                 if (e.ColumnsInfo[i].FieldName=="NSRMC")
                {
                    e.ColumnsInfo[i].ColumnWidth= 320 ;
                }
                
                 if (e.ColumnsInfo[i].FieldName=="FFinancePropShortName")
                {
                    e.ColumnsInfo[i].ColumnWidth= 80 ;
                }
                 if (e.ColumnsInfo[i].FieldName=="FAmount")
                {
                    e.ColumnsInfo[i].ColumnWidth= 140 ;
                }
                 if (e.ColumnsInfo[i].FieldName=="FFileName")
                {
                    e.ColumnsInfo[i].ColumnWidth= 250 ;
                }
            }
          
        }
        void generator_CustomizeColumn(object source, ControlCustomizationEventArgs e)
        {
            if (e.FieldName == "FAmount")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleRight;

            }

            if (e.FieldName == "NSRSBH")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
            if (e.FieldName == "NSRMC")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
        }

        public List<Model.QueryFIssueNo> GetModelVSBZSXX()
        {
            var strWhere = GetWhereString();
            var zsxx = new BLL.QueryFIssueNo().GetModelList(strWhere);
            foreach (Model.QueryFIssueNo q in zsxx)
            {
                if (!string.IsNullOrEmpty(q.FAmount.ToString()))
                {
                    q.FAmount = decimal.Parse(q.FAmount.ToString()) / 10000;
                }
               
            }

            return zsxx;
        }
        [HttpPost]
        public ActionResult Index(string id)
        {
            var vsbzsxx = GetModelVSBZSXX();
          //  return View(vsbzsxx);
            return View();
        }

        [HttpGet]
        public ActionResult Index()
        {
            GetWhereString();
            // var vsbzsxx = GetModelVSBZSXX();
            return View();
        }

        public ActionResult QueryFIssueNoListPartial()
        {
            GetWhereString();
            var vsbzsxx = GetModelVSBZSXX();
            return View(vsbzsxx);
        }
        public ActionResult Print()
        {
            GetWhereString();

            // CreatReport(table);
            return View("Print");
        }
        #region 导出EXCEL
        public ActionResult ExportToXls()
        {
            var table = GetModelVSBZSXX();
            return GridViewExtension.ExportToXls(ExportGridViewSettings, table, HttpUtility.UrlEncode("期号查询导出.xls"), true);
        }

        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings
        {

            get
            {

                if (exportGridViewSettings == null)

                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }

        public static GridViewSettings CreateExportGridViewSettings()
        {
            var setting = new GridViewSettings();
            setting.Name = "gvEditing";
            setting.KeyFieldName = "FIssueNo;NSRSBH";
            setting.CallbackRouteValues = new { Controller = "QueryFIssueNo", Action = "QueryFIssueNoListPartial" };
            setting.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            setting.SettingsPager.PageSize = EnterpriseFund.Common.GlobalConstant.CONSTRING_PAGESIZE;

            setting.Columns.Add("FFileNo", "文件号");
            setting.Columns.Add("FIssueNo", "期号");
            setting.Columns.Add("NSRSBH", "纳税人识别号");
            setting.Columns.Add("NSRBM", "纳税人编码");
            setting.Columns.Add("NSRMC", "企业名称");
            setting.Columns.Add("FFinancePropShortName", "资金性质");
            setting.Columns.Add("FAmount", "补助金额(万元)");
            setting.Columns.Add("FFileName", "备注");
            setting.SettingsBehavior.AllowFocusedRow = true;
            setting.SettingsLoadingPanel.Text = "数据读取中...";

            setting.SettingsExport.Styles.Header.BackColor = System.Drawing.Color.White;
            setting.SettingsExport.Styles.Header.ForeColor = System.Drawing.Color.Black;
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Count, "FFileNo").DisplayFormat = "总计:";
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAmount").DisplayFormat = "￥{0}";
            setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAmount").DisplayFormat = "合计:￥{0}";
            setting.Settings.ShowGroupPanel = true;
            setting.Settings.ShowFooter = true;
            setting.ClientSideEvents.BeginCallback = "function(s,e){OnBeginCallback(s,e)}";
            setting.ClientSideEvents.EndCallback = "function(){}";
            setting.SettingsBehavior.EnableCustomizationWindow = true;
            setting.DataBinding = (sender, e) =>
            {
                ASPxGridView grid = (ASPxGridView)sender;
                for (int i = 0; i < grid.AllColumns.Count; i++)
                {
                    if (grid.AllColumns[i].HeaderStyle.HorizontalAlign == System.Web.UI.WebControls.HorizontalAlign.NotSet)
                    {
                        grid.AllColumns[i].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                    }
                }
            };
            setting.PreRender = setting.BeforeGetCallbackResult = (sender, e) =>
            {


                MVCxGridView gridView = sender as MVCxGridView;
                MVCxGridViewState gridViewState = new MVCxGridViewState(gridView);
                OperateSession.SetToCache(gridViewState, "QueryFIssueNo");


            };
            return setting;
        }

        #endregion

    }
}
